package com.example.pda.net;

import android.net.ParseException;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.NotSerializableException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import retrofit2.HttpException;

public class ApiException extends Exception {

    /**
     * 未知错误
     */
    public static final int UNKNOWN = 1000;
    /**
     * 解析错误
     */
    public static final int PARSE_ERROR = UNKNOWN + 1;
    /**
     * 网络错误
     */
    public static final int NETWORD_ERROR = PARSE_ERROR + 1;
    /**
     * 协议出错
     */
    public static final int HTTP_ERROR = NETWORD_ERROR + 1;

    /**
     * 证书出错
     */
    public static final int SSL_ERROR = HTTP_ERROR + 1;

    /**
     * 连接超时
     */
    public static final int TIMEOUT_ERROR = SSL_ERROR + 1;

    /**
     * 调用错误
     */
    public static final int INVOKE_ERROR = TIMEOUT_ERROR + 1;
    /**
     * 类转换错误
     */
    public static final int CAST_ERROR = INVOKE_ERROR + 1;
    /**
     * 请求取消
     */
    public static final int REQUEST_CANCEL = CAST_ERROR + 1;
    /**
     * 未知主机错误
     */
    public static final int UNKNOWNHOST_ERROR = REQUEST_CANCEL + 1;

    /**
     * 空指针错误
     */
    public static final int NULLPOINTER_EXCEPTION = UNKNOWNHOST_ERROR + 1;

    public static final int UNKNOWNSERVICE_ERROR = NULLPOINTER_EXCEPTION + 1;

    /**
     * 未知错误
     */
    public static final int REQUEST_FAILURE = 999;
    private final int code;
    private String message;

    public ApiException(Throwable throwable, int code) {
        super(throwable);
        this.code = code;
        this.message = throwable.getMessage();
    }

    public int getCode() {
        return code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ApiException(httpException, httpException.code());
            ex.message = "网络错误" + httpException.code();
            return ex;
        } else  if (e instanceof JsonParseException
                || e instanceof ParseException
                || e instanceof JSONException
                || e instanceof JsonSerializer
                || e instanceof NotSerializableException
                ) {
            ex = new ApiException(e, PARSE_ERROR);
            ex.message = "解析错误";
            return ex;
        } else

            if (e instanceof ClassCastException) {
            ex = new ApiException(e, CAST_ERROR);
            ex.message = "类型转换错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ApiException(e, NETWORD_ERROR);
            ex.message = "连接失败";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new ApiException(e, SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof ConnectTimeoutException) {
            ex = new ApiException(e, TIMEOUT_ERROR);
            ex.message = "连接超时";
            return ex;
        } else if (e instanceof java.net.SocketTimeoutException) {
            ex = new ApiException(e, TIMEOUT_ERROR);
            ex.message = "连接超时或无法连接到服务器";
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex = new ApiException(e, UNKNOWNHOST_ERROR);
            ex.message = "无法解析该域名";
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new ApiException(e, NULLPOINTER_EXCEPTION);
            ex.message = e.getMessage();
            return ex;
        } else if (e instanceof UnknownServiceException) {
            ex = new ApiException(e, NULLPOINTER_EXCEPTION);
            ex.message = "网络安全策略不允许,请设置正确的IP地址";
            return ex;
        }  else {
            ex = new ApiException(e, UNKNOWN);
            ex.message = e.getMessage();
            return ex;
        }
    }

}
